@extends('layout.adminlayout')
@section('css')
    <script src="/admin/js/jquery-1.11.1.min.js"></script>
    <script src="/admin/js/metisMenu.min.js"></script>
    <script src="/admin/js/custom.js"></script>
    <link href="/admin/css/custom.css" rel="stylesheet">
    <link href="/admin/css/add_manage.css" rel="stylesheet">
    <style type="text/css">
        /* #sub_menu_1,#sub_menu_2,#sub_menu_3{
           display: none;
         } */
        ul li:hover{
            cursor: pointer;
        }
        li{
            list-style:none
        }
        #j_tb li{
            margin-left: 1em;
        }
    </style>
@endsection
@section('content')
    <div class="forms">
        <div class=" form-grids row form-grids-right">
            <div class="widget-shadow " data-example-id="basic-forms">
                <div class="form-title">
                    <h4>编辑账号管理权限 :</h4>
                </div>
                <div class="form-body">
                    {{csrf_field()}}
                    <input type="hidden" id="accountId" value="{{$id}}">
                    <ul>
                        <li ><input type="checkbox" id="j_cbAll" />分配管理区域
                            <ul id="j_tb">
                                @foreach($data as $v)
                                <li><input type="checkbox" class="single" value="{{$v['a_id']}}" @if(in_array($v['a_id'],$scope))checked @endif/>{{$v['a_name']}}</li>
                                @endforeach
                            </ul>
                        </li>
                    </ul>
                    <input type="button" id="sub" value="submit" style="margin-left: 1em;margin-top: 1em;background-color: #337ab7" >
                    {{--{{csrf_field()}}
                    <input type="hidden" id="accountId" value="{{$id}}">
                    <h4><input type="checkbox" id="multi" name="all"/>分配管理区域</h4><br>
                    @foreach($data as $v)
                    <input type="checkbox" class="single" value="{{$v['a_id']}}" @if(in_array($v['a_id'],$scope))checked @endif/>{{$v['a_name']}}<br>
                    @endforeach
                    <input type="button" id="sub" value="submit">--}}
                </div>
            </div>
        </div>
    </div>
    <script>
        var all = document.getElementById("j_cbAll");
        var tbody = document.getElementById("j_tb");
        var checkboxs = tbody.getElementsByTagName("input");
        all.onclick = function() {
            for (var i = 0; i < checkboxs.length; i++) {
                var checkbox = checkboxs[i];
                checkbox.checked = this.checked;
            }
        };
        for (var i = 0; i < checkboxs.length; i++) {
            checkboxs[i].onclick = function() {
                var isCheckedAll = true;
                for (var i = 0; i < checkboxs.length; i++) {
                    if (!checkboxs[i].checked) {
                        isCheckedAll = false;
                        break;
                    }
                }
                all.checked = isCheckedAll;
            };
        }
        $('#sub').click(function () {
            var accountId = $('#accountId').val();
            var data = new Array();
            $('.single:checked').each(function () {
                var val = $(this).val();
                data.push(val);
            })
            $.ajax({
                type:'post',
                url:'/admin/staff/auth',
                async:false,
                data:{
                    "_token": $("input[name=_token]").val(),
                    'accountId':accountId,
                    'data':data
                },
                dataType:'json',
                success:function(data){
                    if(data.status == 1){
                        location.href = '/admin/staff/index';
                    }
                }
                })
        })
    </script>
@endsection
